package com.xianniu.ltc.strategy.repository;

import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.engine.ID;
import cn.devezhao.persist4j.engine.StandardRecord;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.privileges.bizz.Department;
import com.xianniu.ltc.feign.api.ApiService;
import com.xianniu.ltc.feign.api.AuthService;
import com.xianniu.ltc.model.po.AuthOrganizationPO;
import com.xianniu.ltc.strategy.ExteriorStrategyService;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author WangRuiJie
 * @Title: DepartmentStrategyRepository
 * @Package com.rebuild.web.strategy.repository
 * @Description: 部门策略实现类
 * @date 2021/5/274:51 下午
 */
@Slf4j
@Service
public class DepartmentStrategyRepository implements ExteriorStrategyService {

    @Autowired
    private AuthService authService;
    @Override
    public void sendEntityInfo(Record record,String xnToken) {
        // 每次新增，修改传输全公司和默认部门这两个部门
        log.info("aa=========================================ß"+ JSON.toJSON(record));
        ID deptId = record.getID("deptId");
        String name = record.getString("name");
        String tenantCode = record.getString("tenantCode");
        ID parentDept = record.getID("parentDept");
        boolean isDisabled = false;
        if (record.getBoolean("isDisabled") != null){
            isDisabled = record.getBoolean("isDisabled");
        }
        // 请求地址
        AuthOrganizationPO po = new AuthOrganizationPO();
        po.setOrgId(deptId.toString());
        po.setOrgName(name);
        if (parentDept != null){
            po.setParentOrgId(parentDept.toString());
        }
        po.setSaasCode(tenantCode);
        po.setUseYn(isDisabled ? 0:1);
        log.info("请求auth保存至信息:{}:token：{}", JSON.toJSON(po), UserContextHolder.getToken());
        try {
            ReturnT result = authService.addOrExit(po, UserContextHolder.getToken());
            log.info("请求auth删除组织信息 返回值：{}", result);
            JSONObject jsons = JSONObject.parseObject(JSONObject.toJSONString(result.getData()));
            if (jsons.getInteger("code") != 200 ){
                log.info("请求auth保存至信息报错：{}",jsons);
            }else {
                log.info("请求auth保存至信息成功：{}",jsons);
            }
        }catch (Exception e){
            log.info("请求auth保存至信息异常：{}",e.getMessage());
        }
    }

    @Override
    public void deleteEntityInfo(ID recordId,String xnToken, String tenantCode) {
        try {
            AuthOrganizationPO po = new AuthOrganizationPO();
            po.setOrgId(String.valueOf(recordId));
            po.setSaasCode(tenantCode);
            log.info("请求auth删除组织信息:{},token:{}", JSON.toJSON(po), UserContextHolder.getToken());
            ReturnT result = authService.del(po, UserContextHolder.getToken());
            log.info("请求auth删除组织信息 返回值：{}", result);
            JSONObject jsons = JSONObject.parseObject(JSONObject.toJSONString(result.getData()));
            if (jsons.getInteger("code") != 200 ){
                log.info("请求auth删除组织信息报错：{}",jsons);
            }
        }catch (Exception e){
            log.info("请求auth保存至信息异常：{}",e.getMessage());
        }
    }

    @Override
    public String getEntityCode() {
        return "Department";
    }
}
